package com.langsin.zhangruizhi.mapper;

import com.langsin.bean.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.swing.*;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MyTest {
    SqlSession sqlSession = null;
    //每次单元测试类之前执行
    @Before
    public void before() throws Exception{
        //读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //工厂
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        //获取一个会话（连接）默认不提交
        sqlSession = sqlSessionFactory.openSession();
    }
    //每次单元测试类之后执行
    @After
    public void after() throws Exception{
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void ListTest() throws Exception{
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(9003);
        ids.add(9004);
        ids.add(9005);

        List<Goods> goods = goodsMapper.queryByIds(ids);
        for (Goods good : goods) {
            System.out.println(good);
        }

        }
    @Test
    public void ManyConditionQuery() throws Exception{
        Goods goods = new Goods();
        goods.setGoodsName("%C%");
        goods.setShopPrice(18.0);
        goods.setGoodsStock(900);

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goodsList = goodsMapper.queryByCondition(goods);

        goodsList.forEach(System.out::println);

    }
    @Test
    public void DoubleChartQuery() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserOrders(702);
        System.out.println(user);

        for (Orders order:user.getOrdersList()) {
            System.out.println(order);
        }
    }
    @Test
    public void UpdateWorkerByCondition() throws Exception{
        Worker worker = new Worker();
        worker.setWid("4792a048-5e5d-11e9-9dc0-28d244d68b7c");
        worker.setWname("大律师");
        worker.setAge(45);
        worker.setSex("男");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.updateWorkerByCondition(worker);

    }
    @Test
    public void testOrdersUser() throws Exception{
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        Orders orders = ordersMapper.queryOrdersAndUserByOrderId(1002);
        System.out.println(orders);
        System.out.println(orders.getUser());
    }
    @Test
    public void testuseGoods() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user);
        System.out.println("==========================");
        for (Orders order:user.getOrdersList()) {
            for (OrderDetail orderDetail:order.getOrderDetailList()) {
                System.out.println(orderDetail.getGoods());
            }
        }
    }
    @Test
    public void testOrdersUser2() throws Exception{
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        Orders orders = ordersMapper.queryOrdersAndUserByOrderId(1003);
        System.out.println(orders.getOrderId()+"~"+orders.getOrderDate());

        Thread.sleep(1000);

        System.out.println(orders.getUser());
    }
    @Test
    public void testUpdateFoodsLazy() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(704);
        System.out.println(user.getCname()+","+user.getAddress());

        Thread.sleep(5000);

        for (Orders orders:user.getOrdersList()) {
            System.out.println("订单id: " + orders.getOrderId());

            List<OrderDetail> details = orders.getOrderDetailList();
            Thread.sleep(5000);

            for (OrderDetail detail:details) {
                System.out.println(detail.getGoods());
            }
        }

    }

    @Test
    public void testUseGoodsLazy() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user.getCname() + "," + user.getAddress());

        Thread.sleep(5000);

        for (Orders orders:user.getOrdersList()) {
            System.out.println(orders);
        }
    }

}
